<script setup>

import {Lock, User} from "@element-plus/icons-vue";
import {reactive, ref} from "vue";
import router from "@/router/router.js";
import {post} from "@/net/index.js";
import {ElMessage} from "element-plus";


const registerFormRef = ref();
const registerForm = reactive({
  username: '',
  password: '',
  passwordConfirm: '',
})

const validatorPassword = (rule, value, callback) => {
  if (value === '')
    callback(new Error("请再次输入密码"))
  else if (value !== registerForm.password)
    callback(new Error("两次输入的密码不一致"))
  else callback()
}

const registerFormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: ['blur', 'change'] }
  ],
  password: [
    {required: true, message: '请输入原密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度在 6 到 10 个字符', trigger: ['blur', 'change']}
  ],
  passwordConfirm: [
    {validator: validatorPassword, trigger: ['blur']},
  ]
}

const register = () => {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      post('/api/user/register', {...registerForm},
          () => {
            ElMessage.success('注册成功，欢迎加入我们')
            router.push('/')
          },)
    } else {
      ElMessage.error('请检查填写注册信息')
    }
  })
}
</script>

<template>
  <div style="text-align: center;margin: 0 20px">
    <div style="margin-top: 70px">
      <div style="font-size: 25px;font-weight: bold;">注册新用户</div>
      <div style="font-size: 13px;color: gray">欢迎注册我们的平台，请在下方填写相关信息</div>
    </div>
    <div style="margin-top: 50px">
      <el-form :model="registerForm" ref="registerFormRef" :rules="registerFormRules">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" type="text" placeholder="用户名">
            <template #prefix>
              <el-icon>
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="密码">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="passwordConfirm">
          <el-input v-model="registerForm.passwordConfirm" type="password" placeholder="重复密码">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin-top: 50px">
      <el-button type="warning" @click="register()" style="width: 250px" plain>注册</el-button>
    </div>
    <div style="margin-top: 70px;margin-bottom: 30px">
      <span style="font-size: 14px;line-height: 15px;color: gray">已有账号？</span>
      <el-link style="translate: 0 -1px" @click="router.push('/')">立即登录</el-link>
    </div>
    <div style="margin-top: 100px"></div>
  </div>

</template>

<style scoped>

</style>